//
//  SISSAddTenantTakeLookViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/26.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSAddTenantTakeLookViewController.h"

#import "SISSRecommendRentOutViewController.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSTenantTakeLookRecordViewController.h"
#import "SISSTenantProgressModel.h"
#import "SISSTenantTakeLookModel.h"
#import "SISSRentOutModel.h"

@interface SISSAddTenantTakeLookViewController ()

/* 出租模型 */
@property (strong, nonatomic) SISSRentOutModel *rentOutModel;

@end

@implementation SISSAddTenantTakeLookViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.chooseView.leftStr = @"*出租信息";
    self.chooseView.rightPlaceStr = @"请选择出租信息";
    if (self.isPerfectEnter) {
        self.chooseView.rightStr = @"已选择";
        self.dateStr = self.takeLookModel.takelookDate;
        self.chooseView.userInteractionEnabled = NO;
    }
}

- (void)chooseViewChooseAnction {
    
    SISSRecommendRentOutViewController *vc = [[SISSRecommendRentOutViewController alloc] init];
    vc.isProgressEnter = YES;
    vc.title = @"出租";
    vc.completeBlock = ^(SISSRentOutModel *rentOutModel) {
        self.chooseView.rightStr = @"已选择";
        self.rentOutModel = rentOutModel;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)confirmButtonClickAction:(UIButton *)sender {
    
    [super confirmButtonClickAction:sender];
    if (!self.dateStr) {
        [SISSTools showSVTitle:@"请选择看房时间" andTime:1.0 andColor:YES];
        return;
    }
    if (self.dateStr.length > 0 && [PublicUtil compareOneDate:[NSDate date] withAnotherDate:[PublicUtil getNSDateFromSimpleDateString:self.dateStr]] == NSOrderedAscending) {
        [self alertWithMessage:@"看房时间不能大于当前时间"];
        return ;
    }
    if (!self.isPerfectEnter && !self.rentOutModel) {
        [SISSTools showSVTitle:@"请选择出租信息" andTime:1.0 andColor:YES];
        return;
    }
    if (self.images.count == 0) {
        [SISSTools showSVTitle:@"请选择看房照片" andTime:1.0 andColor:YES];
        return;
    }
    if (self.isPerfectEnter) {
        NSDictionary *parameters = @{
                                     @"ltlrId":self.takeLookModel.ltlrId,
                                     @"date":self.dateStr
                                     };
        [kNetworkManager updateLeaseTakeLookRecordWithParameters:parameters imagesArray:self.images successBlock:^(id responseObject) {
            
            if ([responseObject[@"code"] integerValue] != 200) {
                
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [SISSTools showSVTitle:responseObject[@"message"] andTime:1.0 andColor:YES];
                }];
                return ;
            }
            
            if ([responseObject[@"code"] integerValue] == 200) { // 添加成功
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [SISSTools showSVTitle:@"完善成功，等待对方确认。" andTime:1.0 andColor:YES];
                }];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
            
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
    } else {
        
        NSDictionary *parameters = @{
                                     @"arbpId":self.tenantProgressModel.arbpId,
                                     @"arrId":self.tenantProgressModel.rent.ID,
                                     @"rentType":@(1),
                                     @"albpId":self.rentOutModel.albpId,
                                     @"alrId":self.rentOutModel.ID,
                                     @"leaseType":@(self.rentOutModel.leaseType),
                                     @"date":self.dateStr
                                     };
        [kNetworkManager insertLeaseTakeLookRecordWithParameters:parameters imagesArray:self.images successBlock:^(id responseObject) {
            
            if ([responseObject[@"code"] integerValue] != 200) {
                
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [SISSTools showSVTitle:responseObject[@"message"] andTime:1.0 andColor:YES];
                }];
                return ;
            }
            
            if ([responseObject[@"code"] integerValue] == 200) { // 添加成功
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [SISSTools showSVTitle:@"添加成功，等待对方确认。" andTime:1.0 andColor:YES];
                }];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
            
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
